"""
@Project:p8_syl_flask
@File:my_output_json.py
@Author:郑凯乐
@Date:18:51
"""
from json import dumps
from flask import current_app, make_response


def custom_output_json(data, code, headers=None):
    """自定义返回的json格式"""
    settings = current_app.config.get('RESTFUL_JSON', {})
    if current_app.debug:
        settings.setdefault('indent', 4)

    # 重写返回的data格式
    if 'message' not in data:
        data = {
            'message': 'ok',
            'data': data
        }
    dumped = dumps(data, **settings) + "\n"

    resp = make_response(dumped, code)
    resp.headers.extend(headers or {})
    return resp
